//
//  LocalStoreManager.m
//  ZiChanBao
//
//  Created by Senela on 15/7/22.
//  Copyright (c) 2015年 worldunion.com.cn. All rights reserved.
//

#import "LocalStoreManager.h"
#import "DataModel.h"

@implementation LocalStoreManager


static LocalStoreManager *_instance = nil;

+(LocalStoreManager *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[LocalStoreManager alloc] init];
        
    });
    return _instance;
}


#pragma mark --- 将NSString所有的空格及字符串去掉
- (NSString *)removeSpaceAndNewline:(NSString *)str
{
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return temp;
}


/**
 *  在defualt中设置key和value值
 *
 *  @param value 值
 *  @param key   关键字
 *
 */
-(void)setValueInDefault:(id)value withKey:(NSString *)key
{
    NSData *object = [NSKeyedArchiver archivedDataWithRootObject:value];
    [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(UserModel *)getCurrentUserInfoWithKey:(NSString *)key
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    UserModel *info = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return info;
}


//-(CityObject *)getCurrentCityWithKey:(NSString *)key
//{
//    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
//    CityObject *info = [NSKeyedUnarchiver unarchiveObjectWithData:data];
//    return info;
//}


/**
 *  根据key获取对应的defualt中的value值
 *
 *  @param key
 *
 *  @return
 */
-(id)getValueArrFromDefaultWithKey:(NSString *)key
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSArray  *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    NSMutableArray *valueArr = [NSMutableArray array];
    for(NSDictionary *dic in arr)
    {
        [valueArr addObject:[dic objectForKey:@"value"]];
    }
    
    return valueArr;
}

-(id)getValueFromDefaultWithKey:(NSString *)key
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    id  value = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    return value;
}

-(NSString *)getValueByCode:(NSString *)code type:(NSString *)type
{
    NSArray *arr = [self getValueFromDefaultWithKey:type];
    for(NSDictionary *dic in arr)
    {
        if([[dic objectForKey:@"code"] isEqualToString:code])
            return [ dic objectForKey:@"value"];
    }
    return  nil;
}
-(NSString *)getCodeByValue:(NSString *)value type:(NSString *)type
{
    NSArray *arr = [self getValueFromDefaultWithKey:type];
    for(NSDictionary *dic in arr)
    {
        if([[dic objectForKey:@"value"] isEqualToString:value])
            return [ dic objectForKey:@"code"];
    }
    return  nil;
}

@end
